package com.itheima.health.job;

import com.itheima.health.constants.RedisConstant;
import com.itheima.health.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

public class PictureCleanner {
    @Autowired
    JedisPool jedisPool;

    public void clean() {

        // 获取jedis对象
        Jedis jedis = jedisPool.getResource();
        // 比较集合差
        Set<String> sdiff = jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null && sdiff.size() > 0) {
            // 遍历集合,删除七牛云图片
            for (String s : sdiff) {
                QiniuUtils.deleteFileFromQiniu(s);
                System.out.println("已删除图片: " + s + ",时间:" + new Date());
            }
            // 清空数据库缓存图片信息
            jedis.flushAll();
        }
    }
}
